<?php
/**
 * Nuo.cn Jzp
 */

class CaptureTrace
{
    public static function capture()
    {
        $tracelinks = debug_backtrace();
        $cnt = 1;
        $result = false;
        foreach($tracelinks as $trace){
            $traceInfo = "#{$cnt} {$trace['file']}({$trace['line']}): ";
            # add class name if exists.
            # add operation sign
            if(isset($trace['type']) && $trace['type'] && $trace['type'] != 'nothing')
            {
                $traceInfo .= $trace['class'] . $trace['type'];
            }
            
            $traceInfo .= "{$trace['function']}(";
            
            # add args
            if(isset($trace['args'])) {
                $a = '';
                foreach($trace['args'] as $arg){
                    $traceInfo .= $a;
                    if(is_string($arg)){
                        $traceInfo .= "'{$arg}'";
                    }
                    else if(is_numeric($arg)){
                        $traceInfo .= $arg;
                    }
                    else if(is_array($arg)){
                        $traceInfo .= 'Array';
                    }
                    else if(is_object($arg)){
                        $traceInfo .= get_class($arg);
                    }
                    else{
                        $traceInfo .= $arg;
                    }
                    $a = ', ';
                }
            }
            $traceInfo .= ")";
            $result[] = $traceInfo;
            $cnt++;
        }
        return $result;
    }

    public static function getMessage($concat = "\r\n")
    {
        $traces = self::capture();
        $result = '';
        if($traces)
        {
            foreach($traces as $trace){
                $result .= ($trace . $concat);
            }
        }
        return $result;
    }
}

